package com.zzy.demo.config;

import java.io.File;
import java.io.FileNotFoundException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.zzy.demo.config.interceptor.DataInterceptor;

/**
 * 
 * @author guokaige
 *
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
	
	@Autowired
	DataInterceptor dataInterceptor;

	
	/**
	 * 添加json转换
	 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		/**
		 * 1.定义一个Converter 转换消息 2.添加fastjson配置信息, 比如 是否格式化数据 3.在Converter中添加配置信息
		 * 4.将Converter 添加到 List converters当中
		 */

		// 1.定义一个Converter 转换消息
		FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
		// 2.添加fastjson配置信息, 比如 是否格式化数据
		FastJsonConfig fastJsonConfig = new FastJsonConfig();
		fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
		fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);
		fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
		fastJsonConfig.setCharset(Charset.forName("UTF-8"));
		// 3.在Converter中添加配置信息
		fastConverter.setFastJsonConfig(fastJsonConfig);
		// 4.将Converter 添加到 List converters当中
		converters.add(fastConverter);

	}

	/**
	 * 注册拦截器
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// addPathPatterns 用于添加拦截规则
		// excludePathPatterns 用户排除拦截
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(dataInterceptor);
        interceptorRegistration.excludePathPatterns("/static/**");
        interceptorRegistration.excludePathPatterns("/plugin/**");
        interceptorRegistration.excludePathPatterns("/img/**");
        interceptorRegistration.excludePathPatterns("/js/**");
        interceptorRegistration.addPathPatterns("/**");
        
//        InterceptorRegistration sessionInterceptorRegistration = registry.addInterceptor(authInterceptor);
//        sessionInterceptorRegistration.excludePathPatterns("/static/**");
//        sessionInterceptorRegistration.excludePathPatterns("/plugin/**");
//        sessionInterceptorRegistration.excludePathPatterns("/img/**");
//        sessionInterceptorRegistration.excludePathPatterns("/js/**");
//        sessionInterceptorRegistration.addPathPatterns("/**");
	}

	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

		String baseRealPath=getRealClasspathPath();
		//classpath的真实路经 + 上传相对路径
		String fullMedicalRealPath=ResourceUtils.FILE_URL_PREFIX+baseRealPath+"/picture/";
		
		System.out.println("-------------fullRealPath-------------"+fullMedicalRealPath+"------------------------");
		
		// 将所有/** 访问都映射到    指定文件目录 下
		registry.addResourceHandler("/**")
				.addResourceLocations(fullMedicalRealPath)
				.addResourceLocations("classpath:/static/")
				.setCacheControl(CacheControl.maxAge(30,TimeUnit.DAYS).cachePublic());
		
	}

	
	/**
	 * 获取classpath真实路径
	 * @return
	 */
	public  String  getRealClasspathPath() {
		//classpath的真实路经
		File path = null;
		try {
			path = new File(ResourceUtils.getURL("classpath:").getPath());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		if(!path.exists()) path = new File("");
		String baseRealPath = path.getAbsolutePath();
		baseRealPath=baseRealPath.replaceAll("\\\\", "/");
		return baseRealPath;
	}


}
